using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace MobileTDL
{
	public partial class FileManagerForm : Form
	{
		private const string SettingsFilename = @"ToDoList\todolist.config.xml";

		private Settings _settings;

		public FileManagerForm()
		{
			InitializeComponent();
		}

		public Settings Settings
		{
			get { return _settings; }
			set
			{
				_settings = value;
				lvFiles.Items.Clear();
				if (_settings != null)
				{
					foreach (string favorite in _settings.Favorites)
					{
						lvFiles.Items.Add(FillItem(null, favorite));
					}
				}
			}
		}

		private ListViewItem FillItem(ListViewItem item, string filename)
		{
			if (item == null) item = new ListViewItem();
			item.Tag = filename;
			if (item.SubItems.Count > 0) item.SubItems.Clear();
			item.Text = System.IO.Path.GetFileNameWithoutExtension(filename);
			item.SubItems.Add(System.IO.Path.GetDirectoryName(filename));
			return item;
		}

		private void HandleException(Exception ex)
		{
			MessageBox.Show(ex.Message, Text, MessageBoxButtons.OK, MessageBoxIcon.Exclamation, 
                MessageBoxDefaultButton.Button1);
		}

		private void FileManagerForm_Load(object sender, EventArgs e)
		{
			try
			{
				Settings = Settings.Load<Settings>(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SettingsFilename));
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void btnNew_Click(object sender, EventArgs e)
		{
			try
			{
				MainForm frm = new MainForm();
				frm.CurrentFile = new ToDoListFile();
				frm.Settings = Settings;
				frm.ShowDialog();
				if (!Settings.Favorites.Contains(frm.CurrentFilename))
				{
					Settings.Favorites.Remove(frm.CurrentFilename);
					lvFiles.Items.Add(FillItem(null, frm.CurrentFilename));
				}
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
				openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
				if (openFileDialog1.ShowDialog() == DialogResult.OK)
				{
					Settings.Favorites.Add(openFileDialog1.FileName);
					lvFiles.Items.Add(FillItem(null, openFileDialog1.FileName));
				}
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{
			try
			{
				if (lvFiles.SelectedIndices.Count > 0)
				{
					ListViewItem item = lvFiles.Items[lvFiles.SelectedIndices[0]];
					string filename = (string)item.Tag;
					Settings.Favorites.Remove(filename);
					lvFiles.Items.Remove(item);
				}
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void FileManagerForm_Closing(object sender, CancelEventArgs e)
		{
			try
			{
				Settings.Save(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), SettingsFilename));
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void btnClose_Click(object sender, EventArgs e)
		{
			try
			{
				Close();
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void btnSettings_Click(object sender, EventArgs e)
		{
			try
			{
				SettingsForm frm = new SettingsForm();
				frm.Settings = Settings;
				frm.ShowDialog();
			}
			catch (Exception ex)
			{
				HandleException(ex);
			}
		}

		private void btnOpen_Click(object sender, EventArgs e)
		{
            try
            {
                Cursor.Current = Cursors.WaitCursor;

                if (lvFiles.SelectedIndices.Count > 0)
                {
                    ListViewItem item = lvFiles.Items[lvFiles.SelectedIndices[0]];
                    string filename = (string)item.Tag;
                    MainForm frm = new MainForm();
                    frm.Settings = Settings;
                    frm.CurrentFilename = filename;
                    frm.CurrentFile = new ToDoListFile(filename);
                    Cursor.Current = Cursors.Default;
                    frm.ShowDialog();
                    if (filename != frm.CurrentFilename)
                    {
                        Settings.Favorites.Remove(filename);
                        Settings.Favorites.Remove(frm.CurrentFilename);
                        FillItem(item, frm.CurrentFilename);
                    }
                }
            }
            catch (Exception ex)
            {
                HandleException(ex);
            }
            finally
            {
                Cursor.Current = Cursors.Default;
            }
		}
	}
}